package com.sc.servlet.user.comment;

import com.sc.pojo.BookField;
import com.sc.pojo.Comment;
import com.sc.services.UserServices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: 添加用户评论
 * @author: sc
 * @create: 2023-01-05 01:37
 **/
@WebServlet("/addComment")
public class addCommentServlet extends HttpServlet {

    private UserServices userServices = new UserServices();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String result = "add_suc";
        request.setCharacterEncoding("UTF-8");
        String userid = request.getParameter("userid");
        String fieldid = request.getParameter("fieldid");
        String comment = request.getParameter("comment");
        comment = new String(comment.getBytes("ISO-8859-1"), "utf-8");


        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = dateFormat.format(date);

        Comment commentMes = new Comment();
        commentMes.setComment(comment);
        commentMes.setFieldid(Integer.parseInt(fieldid));
        commentMes.setTime(time);
        commentMes.setUserid(userid);

        userServices.addComment(commentMes);


        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(result);







    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}